package com.xfly.app.ui;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.utils.AllUtils;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.adapter.XsAdapter;
import com.xfly.app.bean.XsBean;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class XsActivity extends BaseMvpActivity {

    TitleBarView title_bar;
    TextView total,yd,wd,time;
    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    ListView listview;
    XsAdapter mXsAdapter;
    LinearLayout timelin;
    PopupWindow popupWindow;
    private View popView;
    Calendar calendar = Calendar.getInstance(Locale.CHINA);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_xs);
    }

    @Override
    public void initViews() {
        total = findViewById(R.id.total);
        yd = findViewById(R.id.yd);
        wd = findViewById(R.id.wd);
        listview = findViewById(R.id.listview);
        timelin = findViewById(R.id.timelin);
        time = findViewById(R.id.time);
        title_bar = findViewById(R.id.title_bar);

        title_bar.setText("销售数据统计");
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
    }

    @Override
    public void initEvents() {
        getdata("","");
        timelin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop();
            }
        });
    }

    private void pop() {
        if (null == popupWindow) {
            if (null == popView) {
                popView = LayoutInflater.from(this).inflate(R.layout.pop_teamtime, null);

                popView.findViewById(R.id.endtime).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                            // 绑定监听器(How the parent is notified that the date is set.)
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                // 此处得到选择的时间，可以进行你想要的操作
//                        tv.setText("您选择了：" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                                ((TextView)popView.findViewById(R.id.endtime)).setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);

                            }
                        }
                                // 设置初始日期
                                , calendar.get(Calendar.YEAR)
                                , calendar.get(Calendar.MONTH)
                                , calendar.get(Calendar.DAY_OF_MONTH)).show();

                    }
                });
                popView.findViewById(R.id.starttime).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                            // 绑定监听器(How the parent is notified that the date is set.)
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                // 此处得到选择的时间，可以进行你想要的操作
//                        tv.setText("您选择了：" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                                ((TextView)popView.findViewById(R.id.starttime)).setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth );

                            }
                        }
                                // 设置初始日期
                                , calendar.get(Calendar.YEAR)
                                , calendar.get(Calendar.MONTH)
                                , calendar.get(Calendar.DAY_OF_MONTH)).show();

                    }
                });

                popView.findViewById(R.id.pay).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            if( TextUtils.isEmpty(((TextView)popView.findViewById(R.id.starttime)).getText().toString())
                                    ||TextUtils.isEmpty(((TextView)popView.findViewById(R.id.endtime)).getText().toString())){
                                showMsg("请选择时间");
                                return;
                            }

                            mXsAdapter = new XsAdapter(getContext(),null);
                            listview.setAdapter(mXsAdapter);
                            popupWindow.dismiss();
                            time.setText(((TextView)popView.findViewById(R.id.starttime)).getText().toString()+" - "
                                    +((TextView)popView.findViewById(R.id.endtime)).getText().toString());
                            getdata(dateToStamp(((TextView)popView.findViewById(R.id.starttime)).getText().toString())
                                    ,dateToStamp(((TextView)popView.findViewById(R.id.endtime)).getText().toString()));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                });

                popView.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popupWindow.dismiss();
                    }
                });
            }
//            View view = LayoutInflater.from(this).inflate(R.layout.personaldata_pop, null);
            popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            popupWindow.setAnimationStyle(R.style.dialogAnim);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
            AllUtils.backgroundAlpha(0.5f, getWindow());
            popupWindow.showAtLocation(popView, Gravity.BOTTOM, 0, 0);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    AllUtils.backgroundAlpha(1.0f, getWindow());
                }
            });
            //popupWindow.showAtLocation(rootView, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        } else {
            AllUtils.backgroundAlpha(0.5f, getWindow());
            popupWindow.showAtLocation(popView, Gravity.BOTTOM, 0, 0);
        }

    }


    private void getdata(String stime,String etime){
        Map<String,String> map = new HashMap<>();
        map.put("stime",stime);
        map.put("etime",etime);
        apiDataRepository.getxs(map, new ApiNetResponse<XsBean>(getContext()) {
            @Override
            public void onSuccess(XsBean data) {
                yd.setText("已到账金额："+data.getTotal1());
                wd.setText("未到账金额："+data.getTotal2());
                total.setText(new BigDecimal(data.getTotal1()).add(new BigDecimal(data.getTotal2())).toEngineeringString());
                mXsAdapter = new XsAdapter(getContext(),data.getList());
                listview.setAdapter(mXsAdapter);
            }
        });
    }


    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

}
